using System.Collections.Generic;
using System.Threading;
using ByteDance.Union;
using ByteDance.Union.Mediation;
using UnityEngine;

/**
 * 模板banner代码示例
 * 注：该接口支持融合功能。并且支持将信息流混出到banner中。
 */
public class ExampleExpressBannerAd
{
    public static void LoadExpressBannerAd()
    {
        AdvCtrlManager.Instance().OnDisposeExpressBannerAd();

        // int width = (int) (UnityEngine.Screen.width / UnityEngine.Screen.dpi * 160);
        // int height = (int)((float)width / 250 * 150); 
        // Debug.Log("CSJM_Unity " + "express banner w: " + width + ", h: " + height + ", dpi: " + (UnityEngine.Screen.dpi/160));

        var adSlot = new AdSlot.Builder()
            .SetCodeId(AdvCtrlManager.CSJ_BANNER_ID) // 必传
            .SetSlideIntervalTime(30) // 单位秒。仅当单独使用csj是生效，启用融合时使用的是Gromore线上配置。
            //期望模板广告view的size,单位dp
            .SetExpressViewAcceptedSize(UnityEngine.Screen.width, 50)
            .SetMediationAdSlot(
                new MediationAdSlot.Builder()
                    .SetBidNotify(true) // 可选
                    .SetScenarioId("unity-SetScenarioId") // 可选
                    .SetWxAppId("unity-wxAppId") // 可选
                    .SetAllowShowCloseBtn(true) // 可选
                    .SetMuted(true)
                    .SetVolume(0.7f)
                    .Build())
            .Build();

        SDK.CreateAdNative().LoadExpressBannerAd(adSlot, new ExpressBannerAdListener());
    }

    public static void ShowExpressBannerAd()
    {
        var ad = AdvCtrlManager.Instance().OnGetExpressBannerAd();
        if (ad == null)
        {
            Debug.LogError("CSJM_Unity "+"请先加载广告");
            return;
        }

#if UNITY_ANDROID
        ad.SetSlideIntervalTime(30 * 1000);
#endif
        ad.SetExpressInteractionListener(new ExpressBannerInteractionListener());
        ad.SetDislikeCallback(new ExpressAdDislikeCallback());
        ad.SetDownloadListener(new AppDownloadListener());
        ad.SetAdInteractionListener(new TTAdInteractionListener());
        ad.ShowExpressAd(0, UnityEngine.Screen.height-100);
    }

    // 广告加载监听器
    public sealed class ExpressBannerAdListener : IExpressBannerAdListener
    {
        public ExpressBannerAdListener()
        {
        }

        public void OnError(int code, string message)
        {
            Debug.Log("CSJM_Unity "+"onExpressAdError: " + message);
        }

        public void OnBannerAdLoad(ExpressBannerAd ad)
        {
            Debug.Log("CSJM_Unity "+"OnExpressBannerAdLoad");
            AdvCtrlManager.Instance().OnGetExpressBannerAd(ad);
            ShowExpressBannerAd();
        }
    }

    // 广告展示监听器
    public sealed class ExpressBannerInteractionListener : IExpressBannerInteractionListener
    {

        public ExpressBannerInteractionListener()
        {
        }

        public void OnAdClicked()
        {
            Debug.Log("CSJM_Unity " + "OnAdClicked");
            AdvCtrlManager.Instance().OnGetExpressBannerAd().UploadDislikeEvent("csjm_unity expressBanner dislike test");
        }

        public void OnAdShow()
        {
            Debug.Log("CSJM_Unity " + "OnAdShow");
        }

        public void OnAdViewRenderError(int code, string message)
        {
            Debug.Log("CSJM_Unity " + "express banner OnAdViewRenderError");
        }

        public void OnAdViewRenderSucc(float width, float height)
        {
            Debug.Log("CSJM_Unity " + "express banner OnAdViewRenderSucc");
        }

        public void OnAdClose()
        {
            Debug.Log("CSJM_Unity " + "OnAdClose");
        }

        public void onAdRemoved()
        {
            Debug.Log("CSJM_Unity " + "onAdRemoved");
        }
    }

    // dislike监听器
    public sealed class ExpressAdDislikeCallback : IDislikeInteractionListener
    {
        public ExpressAdDislikeCallback()
        {
        }

        public void OnCancel()
        {
            Debug.LogError("CSJM_Unity "+"express banner dislike OnCancel");
        }

        public void OnShow()
        {
            Debug.LogError("CSJM_Unity "+"express banner dislike OnShow");
        }

        public void OnSelected(int var1, string var2, bool enforce)
        {
            Debug.LogError("CSJM_Unity "+"express banner dislike OnSelected:" + var2);
            //释放广告资源
            AdvCtrlManager.Instance().OnDisposeExpressBannerAd();
        }
    }
}
